package com.jming.yygh.order.api;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jming.yygh.common.result.Result;
import com.jming.yygh.common.util.AuthContextHolder;
import com.jming.yygh.enums.OrderStatusEnum;
import com.jming.yygh.model.order.OrderInfo;
import com.jming.yygh.order.service.OrderService;
import com.jming.yygh.vo.order.OrderCountQueryVo;
import com.jming.yygh.vo.order.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author jming
 * @version 0.0.1
 * @date 2021/05/14
 */
@Api(tags = "订单管理接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderApiController {

    @Resource
    private OrderService orderService;



    /**
     * 根据日期Id和病人id查询保存订单信息
     * @param scheduleId
     * @param patientId
     * @return Result
     */
    @ApiOperation(value = "根据日期Id和病人id查询保存订单信息")
    @PostMapping("auth/submitOrder/{scheduleId}/{patientId}")
    public Result saveOrder(@PathVariable String scheduleId,
                            @PathVariable Long patientId) {

        Long orderId = orderService.saveOrder(scheduleId, patientId);

        return Result.ok(orderId);
    }


    /**
     * 根据订单id查询订单详细信息
     * @param orderId
     * @return Result
     */
    @ApiOperation(value = "根据订单id查询订单详细信息")
    @GetMapping("auth/getOrders/{orderId}")
    public Result getOrders(@PathVariable String orderId) {

        OrderInfo orderInfo = orderService.getOrder(orderId);
        return Result.ok(orderId);
    }

    /**
     * 订单列表信息(条件查询带分页)
     * @param page
     * @param limit
     * @return Result
     */
    @ApiOperation(value = "订单列表信息(条件查询带分页)")
    @GetMapping("auth/{page}/{limit}")
    public Result list(@PathVariable Long page,
                       @PathVariable Long limit,
                       OrderQueryVo orderQueryVo,
                       HttpServletRequest request) {
        // 设置当前用户id值
        orderQueryVo.setUserId(AuthContextHolder.getUserId(request));

        //
        Page<OrderInfo> orderInfoPage = new Page<>(page, limit);
        IPage<OrderInfo> pageModel = orderService.selectPage(orderInfoPage, orderQueryVo);

        return Result.ok(pageModel);
    }

    /**
     * 获取所有订单状态
     * @return Result
     */
    @ApiOperation(value = "获取所有订单状态")
    @GetMapping("auth/getStatusList")
    public Result getStatusList() {
        return Result.ok(OrderStatusEnum.getStatusList());
    }

    /**
     * 取消预约
     * @param orderId
     * @return Result
     */
    @ApiOperation(value = "取消预约")
    @GetMapping("auth/cnncelOrder/{orderId}")
    public Result cnncelOrder(@PathVariable Long orderId) {

        Boolean isOrder = orderService.cnncelOrder(orderId);

        return Result.ok(isOrder);
    }

    /**
     * 获取订单统计数据
     */
    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("inner/getCountMap")
    public Map<String, Object> getCountMap(@RequestBody OrderCountQueryVo orderCountQueryVo) {
        return orderService.getCountMap(orderCountQueryVo);
    }
}
